package com.fast.manager.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fast.commons.spring.SpringContextHolder;
import com.fast.commons.web.model.AjaxResponse;
import com.fast.commons.web.model.DataGrid;
import com.fast.manager.system.domain.Permit;
import com.fast.manager.system.repository.PermitMapper;
import com.fast.manager.system.service.PermitService;

@Service
public class PermitServiceImpl implements PermitService {
	
	@Autowired
	private PermitMapper mapper ;
	
	@Override
	public AjaxResponse add(Permit record) {
		if(this.mapper.existsFileldValue(null, "fullName", record.getFullName()) > 0) 
			return AjaxResponse.failMsg("该名称已存在！") ;
		if(this.mapper.existsFileldValue(null, "identity", record.getIdentity()) > 0) 
			return AjaxResponse.failMsg("该权限标识已存在！") ;
			
		this.mapper.insertSelective(record);
		
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}

	@Override
	public AjaxResponse delete(long id) {
		this.mapper.deleteByPrimaryKey(id);
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}

	@Override
	public AjaxResponse deleteBatch(Long[] ids) {
		this.mapper.deleteByPrimaryKeys(ids);
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}

	@Override
	public AjaxResponse update(Permit record) {
		if(this.mapper.existsFileldValue(record.getId(), "fullName", record.getFullName()) > 0) 
			return AjaxResponse.failMsg("该名称已存在！") ;
		if(this.mapper.existsFileldValue(record.getId(), "identity", record.getIdentity()) > 0) 
			return AjaxResponse.failMsg("该权限标识已存在！") ;
		
		this.mapper.updateByPrimaryKeySelective(record);
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}

	@Override
	public Permit get(long id) {
		return this.mapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Permit> getAllList(Permit record) {
		return this.mapper.findAllList(record);
	}
	
	@Override
	public DataGrid<Permit> datagrid(Permit conditions) {
		DataGrid<Permit> dg = new DataGrid<Permit>();
		
		Map<String, Object> conditionsMap = new HashMap<String, Object>();
		conditionsMap.put("record", conditions);
		conditionsMap.put("offset", dg.getOffset());
		conditionsMap.put("rows", dg.getPageSize());

		List<Permit> list = this.mapper.findAllListPage(conditionsMap);
		int count = this.mapper.getCount(conditionsMap);
		dg.setRows(list);
		dg.setTotal(count);

		return dg;
	}

	@Override
	public List<Permit> getPermits(String[] ids) {
		return this.mapper.getPermits(ids);
	}

}
